<?php 
		include_once("cmsConfig.php");

		//
		$newFileNo = extractStringWithKeyFromArray("fileNo", $_REQUEST);
		$sessionFileNo = extractStringWithKeyFromArray("fileNo", $_SESSION);
		$newSorts = extractStringWithKeyFromArray("sorts", $_REQUEST);
		$sessionSorts = extractStringWithKeyFromArray("sorts", $_SESSION);
		$newDirection = extractStringWithKeyFromArray("direction", $_REQUEST);
		$sessionDirection = extractStringWithKeyFromArray("direction", $_SESSION);
		$newFormid = extractNumberWithKeyFromArray("formid", $_REQUEST);
		$sessionFormid = extractNumberWithKeyFromArray("formid", $_SESSION);
		$newFolder = extractNumberWithKeyFromArray("folder", $_REQUEST);
		$sessionFolder = extractNumberWithKeyFromArray("folder", $_SESSION);

		
		//we need to add defines here for the modes and what not
		
		define("FILE_POSTINGS", 100);
		define("FILE_VIEW_IMAGE", 101);
		define("FILE_VIEW_FILE", 102);
		define("INTRANET_VIEW_INTRANETPAGE", 103);
		define("FILE_MULTI_UPLOAD", 104);
		define("SITE_VIEW_ORDER", 105);
		
		//**********Stuff Added by Rochelle 2007-05-16*************
		
		define("FILE_BROWSE_QUICK_LINKS", 164);
		define("FILE_BROWSE_QUICK_LINKS_CATEGORIES", 165);
		define("FILE_ADD_QUICK_LINKS", 166);
		define("FILE_ADD_QUICK_LINKS_CATEGORY", 167);
		
		//*********End of Stuff Added By Rochelle******************
		
		define("FILES_AND_IMAGES_ADD", 168);
		define("FILES_AND_IMAGES_VIEW", 169);
		
		
//		define("SMS_LIST_ADD", 224);
//		define("SMS_MULTI_USER_ADD", 225);
//		define("SMS_MULTI_USER_REMOVE", 226);
//		define("SMS_USER_REMOVE", 227);

		define("MAIL_LIST_ADD", 106);		
		define("MAIL_CONTACT_ADD", 223);		
		define("MAIL_LIST_USER_ADD", 107);
		define("MAIL_MULTI_USER_ADD", 108);
		define("MAIL_LIST_BROWSE_FRAME", 109);
		define("MAIL_USER_REMOVE", 110);
		define("MAIL_MULTI_USER_REMOVE", 111);
		define("MAIL_BOUNCE_FWD", 115);
		define("MAILOUT_MAILOUT", 172);
		
		
		define("SITE_WEBPAGEBUILDER",187);//
		
		define("DEFAULT_SET_DEFAULT_MODE",200);
		
		define("SITE_ADD_PRODUCTS", 222);
		
	
		//will add mail out guff here..
		//define("");
		
		/** this is a list of session variables that are to be kept when we purge the session */
		// additional pages/sections will add to this before the purge happens.
		// this is a rather clumsy method of doing things, but due to 
		// the current structure of the app, we kinda need away to remove specific things
		// from the session.
		$protectedSessionVariables = array(
			'fileNo',
			'viewMode',
			'authInfo',
			'sorts',
			'direction',
			'folder'
		);
			
		//try and figure out the form/file that we are looking at number.. 	
		if ( !is_null($newFileNo) ) {
			$fileNo = $newFileNo;
		} else if (!is_null($sessionFileNo)) {
			$fileNo = $sessionFileNo;
		} else {
			$fileNo = 13;
		}
		
		$_SESSION['fileNo'] = $fileNo;
		
		
		//why this here??? need to move to some where else..
		if ( !is_null($newFormid) ) {
			$formid = $newFormid;
		} else if (!is_null($sessionFormid)) {
			$formid = $sessionFormid;
		} else {
			$formid = null;
		}
		$_SESSION['formid'] = $formid;
		
		//why this here??? need to move to some where else..
		if ( !is_null($newFolder) ) {
			$folder = $newFolder;
		} else if (!is_null($sessionFolder)) {
			$folder = $sessionFolder;
		} else {
			$folder = null;
		}
		$_SESSION['folder'] = $folder;
		
		//antonys sorting stuff.. not sure why it is in here..
		if ( !is_null($newSorts) ) {
			if($newSorts != "clear")
				$sorts = $newSorts;
			elseif(key_exists($formid, $CMS_DEFAULT_REPORT_SORTS)) {
				$sorts = $CMS_DEFAULT_REPORT_SORTS[$formid ][0];
			} else {
				$sorts = "datelogged";
			}
		} else if (!is_null($sessionSorts)) {
			$sorts = $sessionSorts;
		} elseif(key_exists($formid, $CMS_DEFAULT_REPORT_SORTS)) {
			$sorts = $CMS_DEFAULT_REPORT_SORTS[$formid ][0];
		} else {
			$sorts = "datelogged";
		}
		$_SESSION['sorts'] = $sorts;
		
		if ( !is_null($newDirection) ) {
			if($newDirection != "clear")
				$direction = $newDirection;
			elseif(key_exists($formid, $CMS_DEFAULT_REPORT_SORTS)) {
				$direction = $CMS_DEFAULT_REPORT_SORTS[$formid][1];
			} else {
			$direction = "DESC";
			}
		} else if (!is_null($sessionDirection)) {
			$direction = $sessionDirection;
		} elseif(key_exists($formid, $CMS_DEFAULT_REPORT_SORTS)) {
			$direction = $CMS_DEFAULT_REPORT_SORTS[$formid][1];
		} else {
			$direction = "DESC";
		}
		$_SESSION['direction'] = $direction;
		// end with antonys
		
		$literalFile=null;
		$viewMode = extractNumberWithKeyFromArray('viewMode', $_SESSION, 0);

		switch ($_SESSION['viewMode']) {
			case PERMISSION_MAILOUT:
				$mode= "Mailout";
				switch($fileNo){
					case MAILOUT_MAILOUT:

						$_SESSION['cmsKvcName'] = 'mailout';
						$_SESSION['cmsKvcClass'] = $CMS_MAILOUT_CLASS;//'cmsmailout';
						
						array_push($protectedSessionVariables, 'cmsKvcName');
						array_push($protectedSessionVariables,'cmsKvcClass');
						array_push($protectedSessionVariables, $_SESSION['cmsKvcName']);
						
						
						$literalFile = "lib/cmsKvcRun.php";
					break;
					case MAIL_LIST_ADD:
						$literalFile = "Mailout/addMailList.php";
					break;
					
//					case SMS_LIST_ADD:
//						$literalFile = "SMS/addSMSList.php";
//					break;

					case MAIL_CONTACT_ADD:
						$literalFile = "Mailout/addMailContact.php";
					break;
					case MAIL_LIST_BROWSE_FRAME:
						$literalFile = "Mailout/browseUsersFrame.php";
					break;
					case MAIL_LIST_USER_ADD:
						$literalFile = "Mailout/addMailListUser.php";
					break;
					
					case MAIL_USER_REMOVE:
						$literalFile = "Mailout/deleteSubscribers.php";
					break;
					
					//case SMS_USER_REMOVE:
					//	$literalFile = "SMS/deleteSubscribers.php";
					//break;
					
					case MAIL_MULTI_USER_REMOVE:
						$literalFile = "Mailout/deleteMultipleMailListUsers.php";
					break;
					
					//case SMS_MULTI_USER_REMOVE:
					//$literalFile = "SMS/deleteMultipleSMSListUsers.php";
					//break;
					
					case MAIL_MULTI_USER_ADD:
						$literalFile = "Mailout/addMultipleMailListUsers.php";
					break;
					
				//	case SMS_MULTI_USER_ADD:
				//		$literalFile = "SMS/addMultipleSMSListUsers.php";
				//	break;
					
					case MAIL_BOUNCE_FWD:
						$literalFile = "Mailout/cmsBounceFwd.php";
					break;
						
				}
			break;
			
			case PERMISSION_INTRANET:
				$mode = "Intranet";
				// switch for intranet things here
				switch ($fileNo){
					//postings here i guess..
					//case something here.
					case FILE_POSTINGS:
						$literalFile = "postings/postingIndex.php";
					break;
					case FILE_VIEW_IMAGE:
						$literalFile = "files/viewImage.php";
					break;						
					case FILE_VIEW_FILE:
						$literalFile = "files/viewFile.php";
					break;
					case INTRANET_VIEW_INTRANETPAGE:
						$literalFile="Intranet/cmsViewIntranetPage.php";
					break;
					case FILE_MULTI_UPLOAD:
						$literalFile="files/cmsAddMultiFiles.php";
					break;
					//*********Stuff Added By Rochelle 2007-05-16********************
					case FILE_BROWSE_QUICK_LINKS:
						$literalFile = "Intranet/cmsBrowseQuickLinks.php";
					break;
					case FILE_BROWSE_QUICK_LINKS_CATEGORIES:
						$literalFile = "Intranet/cmsBrowseQuickLinksCategories.php";
					break;
					case FILE_ADD_QUICK_LINKS:
						$literalFile = "Intranet/cmsAddQuickLinks.php";
					break;
					case FILE_ADD_QUICK_LINKS_CATEGORY:
						$literalFile = "Intranet/cmsAddQuickLinksCategories.php";
					break;
					//*********End of StuffStuff Added By Rochelle 2007-05-16*********
				}
			break;
			//only admin functionality here..
			case PERMISSION_ADMIN:
				$mode = "Admin";
				//admin file switched
				switch ($fileNo){
					
					case 5:
						$literalFile = "Admin/cmsFormEditor.php";
					break;
					
					case 6:
						$literalFile = "Admin/cmsFormSwitchboard.php";

						if($authInfo->usertype != '0') {
							die("FORBIDDEN");
						}


						$formDescriptorTableWriter = new CmsTableWriter( "formdescriptor", $db );
						$formDescriptorTableWriter->resetIterator();

						//does not include the id.. this is pulled from elsewhere..
						$requiredFields = array( "contentmodule", "sidebarmodule", "activemodule");

						//echo "REQUEST = </pre>";print_r($_REQUEST);echo "<pre>";
						$formDescriptor = array();
						if(array_key_exists("formDescriptor", $_REQUEST)) {
							$formDescriptor = $_REQUEST['formDescriptor'];
							//echo "<br>Set Form Descriptor<br>";
						}

						//echo "formDescriptor = </pre>";print_r($formDescriptor);echo "<pre>";
						$save = "";
						if(array_key_exists("save", $_REQUEST)) {
							$save = $_REQUEST["save"];
						}
						if ( $save == "save changes") {

							$formDescriptorTableWriter->resetIterator();
							while ($row = $formDescriptorTableWriter->nextItem()) {		

								$formId =  $row->formid;

								$ir = $formDescriptorTableWriter->getIteratorResultSet();

								if(array_key_exists($row->formid, $formDescriptor)) {
									if ( is_array( $formDescriptor[$row->formid] ))
										$fd = $formDescriptor[$row->formid];
									else 
										$fd = array();
								} else {
									$fd = array();
								}
								$formDescriptorTableWriter->begin();
								$formDescriptorTableWriter->addField( "formid",$formId );
								foreach ( $requiredFields as $key ) {
									if(array_key_exists($key, $fd)) {
										$value = $fd[ $key ]; //echo "key = $key";
									}else {
										$value = 0;
									}
									if (!$value)
										$value = 0;
									else
										$value = 1;

									$formDescriptorTableWriter->addField( $key, $value);
								}
								$formDescriptorTableWriter->saveItem();

								$formDescriptorTableWriter->setIteratorResultSet( $ir );
							}
						}

					break;
					
					case 7:
						$literalFile = "Admin/cmsUserAdmin.php";
					break;
					case 8:
						$literalFile = "Admin/cmsGroupAdmin.php";
					break;
					case 9:
						$literalFile = "Admin/cmsUserAdd.php";
					break;
					case 10:
						$literalFile = "Admin/cmsGroupAdd.php";
					break;
					case 11:
						$literalFile = "Admin/cmsUserDelete.php";
					break;
					case 12:
						$literalFile = "Admin/cmsGroupDelete.php";
					break;
					case 15:
						$literalFile = "Admin/cmsFormExportor.php";
					break;
					case 16:
						$literalFile = "Admin/cmsFormImportor.php";
					break;					
				}
			break;
			case PERMISSION_SITE:
				$mode = "Site";
				//site files switched here
				switch ($fileNo){

					case FILE_VIEW_FILE:
						$literalFile = "files/viewFile.php";
					break;

					case SITE_VIEW_ORDER:
						$literalFile="shop/cmsViewOrder.php";
					break;
					
					case SITE_ADD_PRODUCTS:
						$literalFile="Site/cmsAddMultipleProducts.php";
					break;

					case SITE_WEBPAGEBUILDER:
						
						$_SESSION['cmsKvcName'] = 'pagebuilder';
						$_SESSION['cmsKvcClass'] = 'cmspagebuilder';	
						array_push($protectedSessionVariables, 'cmsKvcName');
						array_push($protectedSessionVariables,'cmsKvcClass');
						array_push($protectedSessionVariables, $_SESSION['cmsKvcName']);
												
						$literalFile = "lib/cmsKvcRun.php";
					break;
									
				}				
			break;
			case '0' :
				$mode = "Default";
				switch ($fileNo) {
					case DEFAULT_SET_DEFAULT_MODE:
						
						$defaultViewMode = extractNumberWithKeyFromArray('defaultViewMode', $_REQUEST, 0);
						
						if ($defaultViewMode != 0 ){
						
							// we need to do a check here also... to make sure that permissions are correct
							// and that the user has the permission to set this as a default??
							// prolly not..
							$_SESSION['viewMode'] = $defaultViewMode;
							unset($_SESSION['fileNo']);
							setcookie('defaultViewMode',$defaultViewMode,time() + (2629744),'/cms/html/',null,null);
							
							redirectCleanly(CMS_HTTP_HOST . CMS_HOME);
						}
					break;
					default:
						// NO OP //
						// we have no mode here .. try and set one to something.. or not??
					break;
				}
			break;
			default:
				
				$mode = "MultiMode";
				//site files switched here
				switch ($fileNo){
					case SITE_VIEW_ORDER:
						$literalFile="shop/cmsViewOrder.php";
					break;	

					case SITE_WEBPAGEBUILDER:
						$_SESSION['cmsKvcName'] = 'pagebuilder';
						$_SESSION['cmsKvcClass'] = 'cmspagebuilder';	
						array_push($protectedSessionVariables, 'cmsKvcName');
						array_push($protectedSessionVariables,'cmsKvcClass');
						array_push($protectedSessionVariables, $_SESSION['cmsKvcName']);
												
						$literalFile = "lib/cmsKvcRun.php";
					break;
									
				}	
				
				
				
		}

		switch ($fileNo) {
			case 1:
				array_push($protectedSessionVariables, 'formid');
				$literalFile = "cmsBrowseContent.php";
			break;
			case 2:
				
				// pick up the form id/document code
				// we were going to do this in the master add.. but that would get messy..
				array_push($protectedSessionVariables, 'documentCodeSave','formIdSave','returnURL');
				$literalFile = extractStringWithKeyFromArray($formid, $CMS_CUSTOM_ADD_PAGES, "cmsMasterAdd.php");
				//$literalFile = "cmsMasterAdd.php";
			break;
			case 4:
				$literalFile = "cmsDelete.php";
			break;
			
			case 14:
				$literalFile = "cmsChangePassword.php";
			break;

			case FILES_AND_IMAGES_ADD:// fileNo 168
				
				$literalFile = "files/cmsFilesModuleAdd.php";
				
			break;	

			case FILES_AND_IMAGES_VIEW:// fileNo 169
				$literalFile = "files/cmsViewFile.php";
			break;

			case FILE_MULTI_UPLOAD:
				$literalFile="files/cmsAddMultiFiles.php";
			break;

			case FILE_VIEW_IMAGE:
				$literalFile = "files/viewImage.php";
			break;

		}
		
		if ( is_null($literalFile) ) {		
			$literalFile = $mode . "/home" . $mode . ".php";
		}
		
		//do the session purge here... after all of the things for the particular session item have been set.
		foreach ($_SESSION as $key =>$value) {
			if (! in_array($key,$protectedSessionVariables, true))
				unset($_SESSION[ $key ]);
		}

		
	//get the content blocks..
	
	//header 
	ob_start();
	include_once("cmsHeader.php");
	$cmsHeaderContent = ob_get_clean();
	
	//view mode
	ob_start();
	include_once("cmsViewMode.php");
	$cmsViewModeContent = ob_get_clean();
	
	//side bar
	ob_start();
	include_once("cmsSideBar.php");
	$cmsSideBarContent = ob_get_clean();
	
	//content
	ob_start();
	
	include_once($literalFile); 
	
	$cmsContentContent = ob_get_clean();
	
	//footer
	ob_start();
	include_once( CMS_BOTTOM_BAR ); 
	$cmsFooterContent = ob_get_clean();	
	
	
	//Set header for whole site to utf-8
	header("Content-Type: text/html; charset=utf-8",true);

	// spit it out in an organised fashion

	//Set header for whole site to utf-8
	header("Content-Type: text/html; charset=utf-8",true);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<meta http-equiv="X-UA-Compatible" content="IE=7" />
	<head>
		<script language="javascript" type="text/javascript" src="/cms/html/js/prototype.js"></script>
		<script>
						window.Gravitate = {
							name: 'Originate',
							version: 'flooby'
						};
						var Gravitate = {
							Gravitate: window.Gravitate
						};
						
						var pe = new PeriodicalExecuter(
						function (pe) {
							new Ajax.Request(
								'/cms/JSON/stayinAlive.php'								
							);
						},
						300);
		</script>																																							</script>		<title><?= TITLE; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<link href="/cms/css/reset.css"  rel="stylesheet" type="text/css" media="screen" />
		<link href="/cms/css/fonts.css"  rel="stylesheet" type="text/css" media="screen" />
		
		<!-- default -->
		<link href="/cms/css/cmslayout.css"  rel="stylesheet" type="text/css" media="screen" />
		

		<script language="javascript" type="text/javascript" src="/cms/html/js/scriptaculous.js"></script>
		<script language="javascript" type="text/javascript" src="/cms/html/js/accordion.js"></script>
		<script language="javascript" type="text/javascript" src="/cms/html/js/swfobject.js"></script>
		<script language="javascript" type="text/javascript" src="/cms/html/js/cms.js" ></script>				
		<script language="javascript" type="text/javascript" src="/cms/html/tiny_mce/tiny_mce.js"></script>
			
				
	</head>
	<body>

	<div id="cms_bodywrapper">	

		<div id="cms_container">

			<div id="cms_top">
				<?= $cmsHeaderContent //these should never alter the state of the machine... in their operations? 
				?>
			</div>
			<div id="cms_status">
				<?= $cmsViewModeContent ?>
			</div>

			<div id="cms_wrapper">
				<div id="cms_sidebar">
					<div id="cms_sidebarIndex">
						<?= $cmsSideBarContent ?>
					</div>
				</div>
				<div id="cms_content">
					<div id="cms_contentIndex">
						<?= $cmsContentContent ?>
					</div>
				</div>
				<div id="cms_left_sidebar"><img src="/cms/images/spacer.gif" height="400" width="1" /></div>
			</div>
			
			<div id="cms_footer" >
				<?= $cmsFooterContent ?>
			</div>

		</div>

	</div>

	</body>
</html>
<?

//myPrintR($_REQUEST);